<?php

session_start();
require 'connection.php';
require('fpdf/PDF.php');
require('phpexcel/Excel.php');

if ($_POST["report"] == "article_category_pdf") {
    article_category_pdf();
} elseif ($_POST["report"] == "article_category_excel") {
    article_category_excel();
} elseif ($_POST["report"] == "article_valued_pdf") {
    article_valued_pdf();
} elseif ($_POST["report"] == "article_valued_excel") {
    article_valued_excel();
} elseif ($_POST["report"] == "article_stock_pdf") {
    article_stock_pdf();
} elseif ($_POST["report"] == "article_stock_excel") {
    article_stock_excel();
}

function article_category_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Artículos por Categoria";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(120, 5, 'Nombre');
    $pdf->setHeaderCol(72, 5, 'Modelo');
    $pdf->Ln();

    $sql = "SELECT id, code, name, model, category_id FROM articles WHERE category_id = :category_id";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("category_id", $_POST["category_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(120, 5, $row[2]);
            $pdf->setDetailCol(72, 5, $row[3]);
            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Artículos por Categoria.pdf", "D");
}

function article_category_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Artículos por Categ'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Nombre')->setColWidth('B', 50);
    $excel->setHeaderCol('C2', 'Modelo')->setColWidth('c', 30);
    $count = 3;
    $sql = "SELECT id, code, name, model, category_id FROM articles WHERE category_id = :category_id";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("category_id", $_POST["category_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, $row[2]);
            $excel->setDetailCol('C' . $count, $row[3]);
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}

function article_valued_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Artículos Valorado";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(65, 5, 'Nombre');
    $pdf->setHeaderCol(27, 5, 'Modelo');
    $pdf->setHeaderCol(20, 5, 'Cantidad');
    $pdf->setHeaderCol(20, 5, 'Precio Compra');
    $pdf->setHeaderCol(20, 5, 'Precio Venta');
    $pdf->setHeaderCol(20, 5, 'Total Compra');
    $pdf->setHeaderCol(20, 5, 'Total Venta');

    $pdf->Ln();

    $sql = "SELECT  name, model, stock, price_purcharse,price_sale FROM articles";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("category_id", $_POST["category_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(65, 5, $row[0]);
            $pdf->setDetailCol(27, 5, $row[1]);
            $pdf->setDetailCol(20, 5, $row[2], 'C');
            $pdf->setDetailCol(20, 5, $row[3], 'C');
            $pdf->setDetailCol(20, 5, $row[4], 'C');
            $pdf->setDetailCol(20, 5, number_format($row[2] * $row[3], 2, '.', ''), 'C');
            $pdf->setDetailCol(20, 5, number_format($row[2] * $row[4], 2, '.', ''), 'C');
            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Artículos Valorado.pdf", "D");
}

function article_valued_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Artículos Valorado'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Nombre')->setColWidth('B', 50);
    $excel->setHeaderCol('C2', 'Modelo')->setColWidth('C', 30);
    $excel->setHeaderCol('D2', 'Cantidad')->setColWidth('D', 20);
    $excel->setHeaderCol('E2', 'Precio Compra')->setColWidth('E', 20);
    $excel->setHeaderCol('F2', 'Precio Venta')->setColWidth('F', 20);
    $excel->setHeaderCol('G2', 'Total Compra')->setColWidth('G', 20);
    $excel->setHeaderCol('H2', 'Total Venta')->setColWidth('H', 20);



    $count = 3;
    $sql = "SELECT  name, model, stock, price_purcharse,price_sale FROM articles";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("category_id", $_POST["category_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, $row[0]);
            $excel->setDetailCol('C' . $count, $row[1]);
            $excel->setDetailCol('D' . $count, $row[2]);
            $excel->setDetailCol('E' . $count, $row[3]);
            $excel->setDetailCol('F' . $count, $row[4]);
            $excel->setDetailCol('G' . $count, number_format($row[2] * $row[3], 2, '.', ''));
            $excel->setDetailCol('H' . $count, number_format($row[2] * $row[4], 2, '.', ''));
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}

function article_stock_pdf() {
    $pdf = new PDF();
    $pdf->title = "Reporte de Artículos Stock";
    $pdf->AddPage();
    //suma de col 192
    $pdf->setHeaderCol(85, 5, 'Nombre');
    $pdf->setHeaderCol(27, 5, 'Modelo');
    $pdf->setHeaderCol(40, 5, 'Cantidad');
    $pdf->setHeaderCol(40, 5, 'Cantidad Minima');


    $pdf->Ln();

    $sql = "SELECT  name, model, stock, stock_minimun FROM articles";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("category_id", $_POST["category_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $pdf->setDetailCol(85, 5, $row[0]);
            $pdf->setDetailCol(27, 5, $row[1]);
            $pdf->setDetailCol(40, 5, $row[2], 'C');
            $pdf->setDetailCol(40, 5, $row[3], 'C');

            $pdf->Ln();
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }
    $pdf->Output("Reporte de Artículos Stock.pdf", "D");
}

function article_stock_excel() {
    $excel = new Excel();
    $excel->title('Reporte de Artículos Stock'); //31 max

    $excel->getActiveSheet()->setTitle();
    $excel->setHeaderCol('B2', 'Nombre')->setColWidth('B', 50);
    $excel->setHeaderCol('C2', 'Modelo')->setColWidth('C', 30);
    $excel->setHeaderCol('D2', 'Cantidad')->setColWidth('D', 30);
    $excel->setHeaderCol('E2', 'Cantidad Minima')->setColWidth('E', 30);

    $count = 3;
    $sql = "SELECT  name, model, stock, stock_minimun FROM articles";
    try {
        $stmt = getConnection()->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
        $stmt->bindParam("category_id", $_POST["category_id"]);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
            $excel->setDetailCol('B' . $count, $row[0]);
            $excel->setDetailCol('C' . $count, $row[1]);
            $excel->setDetailCol('D' . $count, $row[2]);
            $excel->setDetailCol('E' . $count, $row[3]);            
            $count++;
        }
        $stmt = null;
    } catch (PDOException $e) {
        print $e->getMessage();
    }

    $excel->output();
}

?>